﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class MobileVisit
    {
        private readonly KeRui.DAL.MobileVisit dal = new KeRui.DAL.MobileVisit();

        public static int Add(KeRui.Model.MobileVisit model)
        {
            return KeRui.DAL.MobileVisit.Add(model);
        }

        public static void AddLog(string Accept, string UserAgent, int SystemCheck, int UserCheck, string UserIp)
        {
            KeRui.Model.MobileVisit model = new KeRui.Model.MobileVisit();
            model.Accept = Accept;
            model.UserAgent = UserAgent;
            model.SystemCheck = SystemCheck;
            model.UserCheck = UserCheck;
            model.CreateTime = DateTime.Now;
            model.CreateIP = UserIp;
            Add(model);
        }

        public List<KeRui.Model.MobileVisit> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.MobileVisit> list = new List<KeRui.Model.MobileVisit>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.MobileVisit item = new KeRui.Model.MobileVisit();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    item.Accept = dt.Rows[i]["Accept"].ToString();
                    item.UserAgent = dt.Rows[i]["UserAgent"].ToString();
                    if (dt.Rows[i]["SystemCheck"].ToString() != "")
                    {
                        item.SystemCheck = int.Parse(dt.Rows[i]["SystemCheck"].ToString());
                    }
                    if (dt.Rows[i]["UserCheck"].ToString() != "")
                    {
                        item.UserCheck = int.Parse(dt.Rows[i]["UserCheck"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    item.CreateIP = dt.Rows[i]["CreateIP"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int ID)
        {
            this.dal.Delete(ID);
        }

        public bool Exists(int ID)
        {
            return this.dal.Exists(ID);
        }

        public List<KeRui.Model.MobileVisit> GetList(string strCondition)
        {
            string strWhere = "";
            string strOrder = "";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("MobileVisit", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.MobileVisit> GetListByCache(string strCondition)
        {
            string cacheKey = "MobileVisitList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strCondition);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.MobileVisit>) cache;
        }

        public KeRui.Model.MobileVisit GetModel(int ID)
        {
            return this.dal.GetModel(ID);
        }

        public KeRui.Model.MobileVisit GetModelByCache(int ID)
        {
            string cacheKey = "MobileVisitModel-" + ID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.MobileVisit) cache;
        }

        public void Update(KeRui.Model.MobileVisit model)
        {
            this.dal.Update(model);
        }
    }
}

